AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Use(activator,caller)
	if self.LastUsed>CurTime() then 
		self.LastUsed = CurTime()+0.3
		return
	end
	self.LastUsed = CurTime()+0.3
	umsg.Start("drugfactorygui", activator)
		umsg.Short(self.Entity:EntIndex())
		umsg.Short(self.Booze)
		umsg.Short(self.Drugs)
		umsg.Short(self.RandomDrugs)
		umsg.Short(self.SDefense)
		umsg.Short(self.SOffense)
		umsg.Short(self.SWeapmod)
		umsg.Short(self.Refmode)
	umsg.End()
end

function ENT:DropDrug()
	local drug = ents.Create("drug_random")
	drug:SetPos(self.Entity:GetPos()+Vector(0,0,50))
	drug:Spawn()
end

function ENT:DropSuperDrug()
	if self.Refmode==0 then
		Notify(self.dt.Owner, 2, 3, "Paid $"..self.PayDay.." from refining super drugs")
		self.dt.Owner:GiveMoney(self.PayDay)
	else
		local stype = "offense"
		if self.Refmode==2 then stype="defense" elseif self.Refmode==3 then stype="weapmod" end
		local drug = ents.Create("drug_superdrug"..stype)
		drug:SetPos(self.Entity:GetPos()+Vector(0,0,50))
		drug:Spawn()
	end
end

function ENT:EjectSuperDrugs()
	for i=1,self.SOffense,1 do
		local drug = ents.Create("drug_superdrugoffense")
		drug:SetPos(self.Entity:GetPos()+Vector((i*10)-10,10,50))
		drug:Spawn()
		drug.Ejection = CurTime() + 3
	end
	self.SOffense=0
	for i=1,self.SDefense,1 do
		local drug = ents.Create("drug_superdrugdefense")
		drug:SetPos(self.Entity:GetPos()+Vector((i*10)-10,0,50))
		drug:Spawn()
		drug.Ejection = CurTime() + 3
	end
	self.SDefense=0
	for i=1,self.SWeapmod,1 do
		local drug = ents.Create("drug_superdrugweapmod")
		drug:SetPos(self.Entity:GetPos()+Vector((i*10)-10,-10,50))
		drug:Spawn()
		drug.Ejection = CurTime() + 3
	end
	self.SWeapmod=0
end

function ENT:DropUberDrug()
	local drug = ents.Create("drug_uberdrug")
	drug:SetPos(self.Entity:GetPos()+Vector(0,0,50))
	drug:Spawn()
end

function ENT:Touch(drug)
	if self.Entity:IsPowered() then
		if(drug.Ejection != nil && drug.Ejection > CurTime()) then return end
		local randamt = 10
		local drugamt = 5
		local boozeamt = 3
		
		if (drug:GetClass()=="drug_booze") then
			self.Booze=self.Booze+1
			if (self.Booze>=boozeamt) then
				self.Booze = 0
				self.Drugs = self.Drugs+1
				if (self.Drugs>=drugamt) then
					self.Drugs = 0
					self.Entity:DropDrug()
				end
			end
			drug.Ejection = CurTime() + 3
			drug:Remove()	
		end
		
		if (drug:GetClass()=="drug_druglabdrug") then
			self.Drugs=self.Drugs+1
			if (self.Drugs>=drugamt) then
				self.Drugs = 0
				self.Entity:DropDrug()
			end
			drug.Ejection = CurTime() + 3
			drug:Remove()	
		end
		
		if (drug:GetClass()=="drug_random") then
			self.RandomDrugs=self.RandomDrugs+1
			if (self.RandomDrugs>=randamt) then
				self.RandomDrugs = 0
				self.Entity:DropSuperDrug()
			end
			drug.Ejection = CurTime() + 3
			drug:Remove()	
		end

		if (drug:GetClass()=="drug_superdrugoffense" && self.SOffense<3) then
			self.SOffense=self.SOffense+1
			drug.Ejection = CurTime() + 3
			drug:Remove()
		end
		if (drug:GetClass()=="drug_superdrugdefense" && self.SDefense<3) then
			self.SDefense=self.SDefense+1
			drug.Ejection = CurTime() + 3
			drug:Remove()
		end
		if (drug:GetClass()=="drug_superdrugweapmod" && self.SWeapmod<3) then
			self.SWeapmod=self.SWeapmod+1
			drug.Ejection = CurTime() + 3
			drug:Remove()
		end
	end
end

function ENT:CanRefine(mode,ply)
	if !self.Entity:IsPowered() && mode=="uber" then
		return "Low power."
	end
	if mode=="eject" || mode=="money" then return true end
	if (mode=="offense" || mode=="defense" || mode=="weapmod") then
			return true
	end
	if mode=="uber" then
		if self.SOffense>=3 && self.SDefense>=3 && self.SWeapmod>=3 then
			return true
		else
			return "Not enough Superdrugs."
		end
	end
	return "You're doing it wrong."
end

function ENT:SetMode(mode)
	if mode=="eject" then
		self.Entity:EjectSuperDrugs()
	end
	if mode=="money" then
		self.Refmode=0
	elseif mode=="offense" then
		self.Refmode=1
	elseif mode=="defense" then
		self.Refmode=2
	elseif mode=="weapmod" then
		self.Refmode=3
	elseif mode=="uber" then
		self.Entity:DropUberDrug()
		self.SOffense=0
		self.SDefense=0
		self.SWeapmod=0
		UberDrugExists()
	end
end

function ENT:OnRemove()
	self.Entity:EjectSuperDrugs()
end

function ccSetRefineryMode(ply, cmd, args)
	if (args[1]==nil || args[2] == nil) then 
		return 
	end
	mode = tostring(args[2])
	if !ValidEntity(ents.GetByIndex(args[1])) || (mode!="money" && mode!="offense" && mode!="defense" && mode!="weapmod" && mode!="eject" && mode!="uber") then
		return 
	end
	local refinery = ents.GetByIndex(args[1])
	if (refinery:GetClass()!="struct_refinery" || ply:GetPos():Distance(refinery:GetPos())>300) then return end
	
	local ref = refinery:CanRefine(mode,ply)
	if (ref==true) then
		refinery:SetMode(mode)
	else
		Notify(ply,4,3,ref)
	end
end
concommand.Add("setrefinerymode", ccSetRefineryMode)